<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta name="generator" content="Doxygen 1.8.13">
<title>OpenCV:在Eclipse中使用OpenCV(插件CDT)</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon">
<link href="../../tabs.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css">
<link href="../../stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../df/d65/tutorial_table_of_content_introduction.html">Introduction to OpenCV</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">在Eclipse中使用OpenCV(插件CDT)</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>上一个教程:</b> <a class="el" href="../../db/df5/tutorial_linux_gcc_cmake.html">Using OpenCV with gcc and CMake</a></p>
<p><b>下一个教程:</b> <a class="el" href="../../d3/d52/tutorial_windows_install.html">Installation in Windows</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">原作者</td><td align="left">Ana Huamán</td></tr>
<tr>
<td align="right">兼容性</td><td align="left">OpenCV &gt;= 3.0</td></tr>
</table>
<dl class="section warning"><dt>警告</dt><dd>本教程可能包含过时的信息:</dd></dl>
<h2>先决条件</h2>
<p>两种方法,一种是直接形成一个项目,另一种是通过CMake先决条件</p><ol type="1">
<li>已安装<a href="http://www.eclipse.org/">Eclipse</a>在工作站中(只需要C/C++的CDT插件).您可以执行以下步骤:<ul>
<li>转到Eclipse站点</li>
<li>下载<a href="http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers/heliossr2">C/C++开发人员的Eclipse IDE</a>. 根据您的工作站选择链接:</li>
</ul>
</li>
<li>安装了OpenCV,如果还没有,就看下这个教程<a class="el" href="../../d7/d9f/tutorial_linux_install.html">here</a>.</li>
</ol>
<h2>做一个项目</h2>
<ol type="1">
<li>启动Eclipse:只需运行文件夹中的可执行文件:</li>
<li><p class="startli">去<b>文件 -&gt; 新建 -&gt; C/C++项目</b></p>
<div class="image">
<img src="../../a0.png" alt="a0.png">
</div>
</li>
<li><p class="startli">为项目选择一个名称(即DisplayImage):<b>空项目</b>这个例子应该没问题:</p>
<div class="image">
<img src="../../a1.png" alt="a1.png">
</div>
</li>
<li>默认情况下保留其他所有内容:点击<b>完成</b>.</li>
<li><p class="startli">您的项目(在本例中是DisplayImage)应该出现在<b>项目导航器</b>(通常在窗户的左边):</p>
<div class="image">
<img src="../../a3.png" alt="a3.png">
</div>
</li>
<li>现在,让我们使用OpenCV添加一个源文件:<ul>
<li><p class="startli">右键单击<b>显示</b>(在导航器中):<b>新建 -&gt; 文件夹</b>.</p>
<div class="image">
<img src="../../a4.png" alt="a4.png">
</div>
</li>
<li>命名文件夹<b>src</b>然后打<b>完成</b></li>
<li>右键单击新创建的<b>src</b>文件夹.选择<b>新建源文件</b>:</li>
<li><p class="startli">就叫它<b>DisplayImage.cpp</b>. 点击<b>完成</b></p>
<div class="image">
<img src="../../a7.png" alt="a7.png">
</div>
</li>
</ul>
</li>
<li>所以,现在您有了一个带有空.cpp文件的项目:让我们用一些示例代码来填充它(换句话说,复制并粘贴下面的代码段):<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;opencv2/opencv.hpp&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">  <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> image;</div><div class="line">  image = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( argv[1], 1 );</div><div class="line"></div><div class="line">  <span class="keywordflow">if</span>( argc != 2 || !image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4d33bed1c850265370d2af0ff02e1564">data</a> )</div><div class="line">    {</div><div class="line">      printf( <span class="stringliteral">&quot;No image data \n&quot;</span> );</div><div class="line">      <span class="keywordflow">return</span> -1;</div><div class="line">    }</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( <span class="stringliteral">&quot;Display Image&quot;</span>, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;Display Image&quot;</span>, image );</div><div class="line"></div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --></li>
<li>我们只缺少最后一步:告诉OpenCV OpenCV头和库在哪里:为此,请执行以下操作:<ul>
<li>到<b>项目&ndash;&gt;属性</b></li>
<li>在<b>C/C++ Build</b>,单击<b>设置</b>. 在右边,选择<b>工具设置</b>选项卡.在这里,我们将输入标题和库信息:<ol type="a">
<li><p class="startli">在<b>GCC C++编译器</b>,转到<b>包括</b>. 在<b>包含路径(-l)</b>应该包括安装opencv的文件夹的路径:在我们的示例中,这是 /usr/local/include/opencv.</p>
<div class="image">
<img src="../../a9.png" alt="a9.png">
</div>
<dl class="section note"><dt>Note</dt><dd>如果您不知道opencv文件在哪里,请打开<b>终点站</b>和类型:<div class="fragment"><div class="line">pkg-config --cflags opencv</div></div><!-- fragment -->例如,该命令给了我以下输出:<div class="fragment"><div class="line">-I/usr/local/include/opencv -I/usr/local/include</div></div><!-- fragment --></dd></dl>
</li>
<li><p class="startli">现在转到<b>GCC C++链接器</b>,必须填充两个空格:</p>
<p class="startli">先进的<b>库搜索路径 (-L)</b>您必须将路径写入opencv库所在的位置,在我的示例中,路径是:</p><pre class="fragment">/usr/local/lib</pre><p class="startli">然后在<b>库(-l)</b>添加您可能需要的OpenCV库:通常只有下面列表中的前4个就足够了(对于简单的应用程序):在我的情况下,我把所有的,因为我计划使用整个一堆:</p>
<p class="startli">opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui opencv_ml opencv_videoio opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_flann</p>
<div class="image">
<img src="../../a10.png" alt="a10.png">
</div>
<p class="startli">如果你不知道你的库在哪里(或者你只是个精神病患者,想确定路径是否正确),请输入<b>终点站</b>:</p><div class="fragment"><div class="line">pkg-config --libs opencv</div></div><!-- fragment --><p>我的输出(如果您想检查)是:</p><div class="fragment"><div class="line">-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann</div></div><!-- fragment --><p>现在你完了:点击<b>好的</b></p>
</li>
</ol>
</li>
</ul>
</li>
</ol>
<ul>
<li><p class="startli">你的项目应该准备好了:为此,请转到<b>项目-&gt;全部生成</b></p>
<p class="startli">在控制台里,你应该得到</p>
<div class="image">
<img src="../../a12.png" alt="a12.png">
</div>
<p class="startli">如果你签入你的文件夹,那里应该有一个可执行文件:</p>
</li>
</ul>
<h2>运行可执行文件</h2>
<p>现在我们有一个可执行文件可以运行了:如果我们使用终端,我们可能会这样做:</p><div class="fragment"><div class="line">cd &lt;DisplayImage_directory&gt;</div><div class="line">cd src</div><div class="line">./DisplayImage ../images/HappyLittleFish.png</div></div><!-- fragment --><p>假设用作参数的图像位于 &lt;显示图像&gt;/images/HappyLittleFish.png. 我们仍然可以这样做,但是让我们从Eclipse开始:</p>
<ol type="1">
<li>去<b>运行-&gt;运行配置</b></li>
<li>在C/C++应用程序下,您将看到可执行文件+调试的名称(如果没有,点击C/C++应用程序几次):选择名称(在本例中为<b>图像调试</b>).</li>
<li><p class="startli">现在,在窗口的右侧,选择<b>论据</b>选项卡:写入要打开的图像文件的路径(相对于工作区/DisplayImage文件夹的路径):让我们使用<b>HappyLittleFish.png(快乐小鱼):</b>:</p>
<div class="image">
<img src="../../a14.png" alt="a14.png">
</div>
</li>
<li><p class="startli">点击<b>应用</b>按钮,然后在运行:一个OpenCV窗口应该弹出鱼的图像(或任何你用过的):</p>
<div class="image">
<img src="../../a15.jpg" alt="a15.jpg">
</div>
</li>
<li>祝贺 你！您已经准备好使用Eclipse享受OpenCV带来的乐趣了:</li>
</ol>
<h3>V2:使用CMake+OpenCV和Eclipse(插件CDT)</h3>
<p>假设你有或创建了一个新文件,<em>helloworld.cpp文件</em>在一个名为<em>foo</em>:</p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;opencv2/opencv.hpp&gt;</span></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main ( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv )</div><div class="line">{</div><div class="line">  <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img(480, 640, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32b18d904ee2b1731a9416a8eef67d06">CV_8U</a>);</div><div class="line">  <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(img, <span class="stringliteral">&quot;Hello World!&quot;</span>, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( 200, 400 ), <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a> | <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11af0f7c8824fe42205076771a7b67661c2">FONT_ITALIC</a>, 1.0, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 255, 255, 0 ));</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;My Window&quot;</span>, img);</div><div class="line">  <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><ol type="1">
<li>创建一个构建目录,比如在<em>foo</em>: mkdir /build. 然后制作cd:</li>
<li>放一个<code>CmakeLists.txt</code>内部版本中的文件:<div class="fragment"><div class="line">PROJECT( helloworld_proj )</div><div class="line">FIND_PACKAGE( OpenCV REQUIRED )</div><div class="line">ADD_EXECUTABLE( helloworld helloworld.cxx )</div><div class="line">TARGET_LINK_LIBRARIES( helloworld \f${OpenCV_LIBS} )</div></div><!-- fragment --></li>
<li>运行: cmake-gui .. 并确保填写opencv的构建位置.</li>
<li>然后单击配置,然后单击生成.如果没事的话,<b>退出 cmake-gui</b></li>
<li>运行<code>make -j4</code>(j4是可选的,它只是告诉编译器在4个线程中构建),确保它建立起来:</li>
<li>启动eclipse:将工作区放在某个目录中,但是<b>不</b>在foo或<code>foo\build</code></li>
<li>在“项目资源管理器”区域中单击鼠标右键:选择导入,然后打开C/C++过滤器:选择<em>现有代码</em>作为Makefile项目:</li>
<li>说出你的项目,比如<em>helloworld</em>. 浏览到现有代码位置<code>foo\build</code>(运行 cmake-gui 的地方):选择<em>Linux GCC</em>在*“索引器设置的工具链”*中按<em>完成</em>.</li>
<li>在“项目资源管理器”区域中单击鼠标右键:选择属性:在C/C++构建下,设置<em>生成目录:</em>从某种程度上说<code>${workspace_loc:/helloworld}</code>到<code>${workspace_loc:/helloworld}/build</code>因为那是你要去的地方:<ol type="a">
<li>您还可以选择修改Build命令:从make修改为<code>make VERBOSE=1 -j4</code>它告诉编译器生成用于调试的详细符号文件,并在4个并行线程中编译:</li>
</ol>
</li>
<li>完成！</li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"><address class="footer"><small>Generated on Fri Apr 2 2021 11:36:36 for OpenCV by zsz<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen">
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
